
-------------------------------------
-- 右鍵菜單增強
-------------------------------------

local locale = GetLocale()

local AddFriend = C_FriendList and C_FriendList.AddFriend or AddFriend or function() end
local SendWho = C_FriendList and C_FriendList.SendWho or SendWho or function() end

local UnitPopupButtonsExtra = {
    ["FOLLOW_UNIT"] = { enUS ="Following", zhCN = "强制跟随", zhTW = "强制跟随" },
    ["SEND_WHO"] = { enUS ="Query Detail",  zhCN = "查询玩家", zhTW = "查詢玩家" },
}


local cancel_follow = CreateFrame("Button", nil, UIParent);
cancel_follow:SetSize(64, 64);
cancel_follow:EnableMouse(true);
cancel_follow:RegisterForClicks("AnyUp");
cancel_follow:SetNormalTexture("interface\\buttons\\ui-grouploot-pass-up");
cancel_follow:SetPoint("CENTER", 0, 250);
cancel_follow:Hide();
local text_stop = cancel_follow:CreateFontString(nil, "OVERLAY");
text_stop:SetFont(GameFontNormal:GetFont(), 18, "OUTLINE");
text_stop:SetPoint("TOP", cancel_follow, "BOTTOM", 0, -4);
text_stop:SetTextColor(1.0, 0.75, 0.0);
text_stop:SetText('点击停止跟随');
text_stop:Show();

local _FOLLOWING_NAME = nil;
C_Timer.NewTicker(0.5, function()
    if _FOLLOWING_NAME then
        FollowUnit(_FOLLOWING_NAME);
    end
end);

cancel_follow:SetScript("OnClick", function()
    cancel_follow:Hide();
    _FOLLOWING_NAME = nil;
end);
cancel_follow:SetScript("OnUpdate", function()
end);

local function start_follow(unit, name)
    _FOLLOWING_NAME = name;
    cancel_follow:Show();
    local _, ec = UnitClass(unit);
    -- if ec then
    --     local color = RAID_CLASS_COLORS[ec];
    --     info:SetText(string.format(" 正在跟随 [\124cff%.2x%.2x%.2x%s\124r]", color.r * 255, color.g * 255, color.b * 255, name));
    -- else
    --     info:SetText(" 正在跟随 [" .. name .. "]")
    -- end
end

-- 获取语言
for k, v in pairs(UnitPopupButtonsExtra) do
    v.text = v[locale] or k
    UnitPopupButtons[k] = v
end

local function popupClick(self, info)
    local editBox
    local name, server = UnitName(info.unit)
    if (info.value == "SEND_WHO") then
		SendWho("n-" .. UIDROPDOWNMENU_INIT_MENU.name);
    elseif (info.value == "FOLLOW_UNIT") then
        start_follow(UIDROPDOWNMENU_INIT_MENU.unit, UIDROPDOWNMENU_INIT_MENU.name);
    else
        print(info.value)
    end
end


hooksecurefunc("UnitPopup_ShowMenu", function(dropdownMenu, which, unit, name, userData)
    if (UIDROPDOWNMENU_MENU_LEVEL > 1) then return end
    if (unit and (unit == "target" or string.find(unit, "party"))) then

        local info = UIDropDownMenu_CreateInfo()
        local title = UIDropDownMenu_CreateInfo()

        title.text = '扩展'
        title.isTitle = true
        UIDropDownMenu_AddButton(title, 1)

        for k, v in pairs(UnitPopupButtonsExtra) do
            v.text = v[locale] or k
            -- print( k ,v.text)
            info.isTitle = false;

            info.text = v.text
            info.arg1 = {
                value=k,
                unit=unit
            }
            info.func = popupClick
            info.notCheckable = true
            UIDropDownMenu_AddButton(info, 1)

        end

        -- local info
        -- info = UIDropDownMenu_CreateInfo()
        -- info.text = UnitPopupButtonsExtra["NAME_COPY"].text
        -- info.arg1 = {value="NAME_COPY",unit=unit}
        -- info.func = popupClick
        -- info.notCheckable = true
        -- UIDropDownMenu_AddButton(info)
    end
end)
